package com.party.web.web.security;

import com.party.common.utils.PasswordUtils;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;

/**
 * 自定义登陆凭证验证
 * party
 * Created by wei.li
 * on 2016/10/28 0028.
 */
public class CustomCredentialsMatcher extends SimpleCredentialsMatcher {

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        if (token instanceof MyUsernamePasswordToken) {
            MyUsernamePasswordToken mupt = (MyUsernamePasswordToken) token;
            if (mupt.getLoginType().equals(LoginType.NOPASSWD.getCode())) {
                return true;
            }
            if (mupt.getLoginType().equals(LoginType.PASSWORD.getCode())) {
                //账户凭证
                String accountCredentials = (String) getCredentials(info);
                //令牌凭证
                String password = String.valueOf(mupt.getPassword());
                return PasswordUtils.validatePassword(password, accountCredentials);
            } else {
                return false;
            }
        } else {
            UsernamePasswordToken mupt = (UsernamePasswordToken) token;
            //账户凭证
            String accountCredentials = (String) getCredentials(info);
            //令牌凭证
            String password = String.valueOf(mupt.getPassword());
            return PasswordUtils.validatePassword(password, accountCredentials);
        }
    }
}
